import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
//    public int serverPort;
//    public String serverIp;
    Socket socket = null;
    public TcpEchoClient(String serverIp,int Port) throws IOException {
        socket = new Socket(serverIp,Port);
    }
    public void start(){
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner scannerinput = new Scanner(inputStream);
            while(true){
                System.out.println(":>");
                //从控制台输入请求
                String request = scanner.next();
                //发送请求
                printWriter.println(request);
                printWriter.flush();
                //读取请求
                String response = scannerinput.next();
                //将请求打印到控制台上
                System.out.printf("res: %s; req: %s\n",request,response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1",8090);
        tcpEchoClient.start();
    }
}
